package com.kaishengit.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.UserService;

import com.kaishengit.util.StringUtil;
import com.kaishengit.util.web.ServletUtil;

public class UserReg extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService service=new UserService();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletUtil.forward(request, response, "/reg");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username=req.getParameter("username");
		String password=StringUtil.sha1(req.getParameter("password"));
		String repassword=StringUtil.sha1(req.getParameter("repassword"));
		String email=req.getParameter("email");
		String isAgree=req.getParameter("isAgree");
		
		if(username!=""&&password!=""&&repassword!=""&&password.equals(repassword)&&email!=""&&isAgree!=null){
			String ip=ServletUtil.getIp(req);
			service.addUser(username,password,email,ip);
			
			ServletUtil.redirect(resp, "login.jspx?state=20001");
		}else{
			ServletUtil.setRequestAttribute(req, "state", 10003);
			ServletUtil.forward(req, resp, "/reg");
		}
	}
}
